package dian.qing.li.demo.security.exception;

import dian.qing.li.demo.security.commons.RestResult;
import dian.qing.li.demo.security.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * RestException EC_liqingdian
     */
    @ExceptionHandler({RestException.class})
    public RestResult ucException(HttpServletRequest request, RestException e) {
        String url = RequestUtils.getRequestURI(request);
        log.error("=====||=====自定义UC异常=====||=====");
        log.error("\n请求路径:{}\n异常信息:{}", url, e.getMsg(), e);
        return new RestResult(e.getCode(), e.getMsg(), null);
    }

    /**
     * Exception EC_liqingdian
     */
    @ExceptionHandler({Exception.class})
    public RestResult exception(HttpServletRequest request, Exception e) {
        String url = RequestUtils.getRequestURI(request);
        log.error("=====||=====系统异常=====||=====");
        log.error("\n请求路径:{}\n异常信息:{}", url, e.getMessage(), e);
        return new RestResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部异常，请联系运维人员处理", null);
    }
}
